﻿using System;
using NHibernate.Mapping.Attributes;

namespace monitor_2_DBwork
{
    [Class(Table = "DeviceLog")]
    class DeviceLog : IRepositoryName
    {    

        [Id(0, Column = "Id", Type = "int", Name = "Id")]
        [Generator(1, Class = "identity")]
        public virtual int Id { get; protected set; }

        [Property(Name = "Severity", Column = "Severity", Type = "String(20)")]
        public virtual string Severity { get; set; }

        [Property(Name = "Name", Column = "Name", Type = "String(100)", UniqueKey = "un1")]
        public virtual string Name { get; set; }

        [Property(Name = "Source", Column = "Source", Type = "String(30)")]
        public virtual string Source { get; set; }

        [Property(Name = "Details", Column = "Details", Type = "String(100)", UniqueKey = "un1")]
        public virtual string Details { get; set; }

        [Property(Name = "Time", Column = "Time", Type = "DateTime", UniqueKey = "un1")]
        public virtual DateTime Time { get; set; }

        [Property(Name = "EndTime", Column = "EndTime", Type = "DateTime")]
        public virtual DateTime? EndTime { get; set; }

        [Property(Name = "DevId", Column = "Devid", Type = "int", UniqueKey = "un1")]
        public virtual int DevId { get; set; }

        [ManyToOne(Name = "Device", Lazy = Laziness.False, Class = "Devices", Column = "Devid",
            Cascade = "save-update", Insert = false, Update = false, NotNull = false)]
        public virtual Devices Device { get; set; }

        public DeviceLog() 
        {
            //ref_Rtps = new Rtps();
        }

        public DeviceLog(string a, string b, string c, string d, DateTime e, int f)
        {
            Severity = a;
            Name = b;
            Source = c;
            Details = d;
            Time = e;
            DevId = f;
            //ref_Rtps = new Rtps();
        }

        public DeviceLog(string[] a, int devId)
        {
            Severity = a[0];
            Name = a[1];
            Source = a[2];
            Details = a[3];
            Time = DateTime.Parse(a[4]);
            if (a.Length > 5) //ТРАЙ!!!!!
            {
                DateTime dt;
                if (DateTime.TryParse(a[5], out dt))
                    EndTime = dt;
                else
                {
                    EndTime = null;
                }
                //EndTime = DateTime.Parse(a[5]);
            }
            else
                EndTime = null;
            DevId = devId;
            //ref_Rtps = new Rtps();
        }
    }
}
